package com.zhengx.fragmentutildemo;


import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.zhengx.fragmentutil.FragController;
import com.zhengx.fragmentutil.FragmentUtil;

public class MainActivity extends AppCompatActivity {

    private FragController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {

        controller = FragmentUtil
                .findController(this);

        Fragment fragment = controller.findFragment(FragmentOne.class);
        if (fragment == null) {
            fragment = new FragmentOne();
        }

        controller.initSingleFragment(fragment);

        findViewById(R.id.tv_pre).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controller.pop();
            }
        });

        findViewById(R.id.btn_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(MainActivity.class.getSimpleName(), controller.getTopFragment().toString());
            }
        });
        findViewById(R.id.btn_stack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(MainActivity.class.getSimpleName(), controller.printFragmentStack());
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (!FragmentUtil.handleFragmentsBackPressed(this)) {
            super.onBackPressed();
        }
    }
}
